package com.example.yilu1;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.gc.materialdesign.views.CheckBox;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingDeque;

public class PurchasingcarActivity extends AppCompatActivity implements View.OnClickListener{

    ListView listView;
    static private List<Good> goodList;
    private ImageButton user_button;
    private FloatingActionButton start_button;
    static public List<LatLng> goodLocations;
    private GoodAdapter adapter;
    private CheckBox allCheck;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_purchasingcar);
        ActionBar actionBar=getSupportActionBar();
        actionBar.hide();
        user_button=(ImageButton)findViewById(R.id.user_button);
        start_button=(FloatingActionButton)findViewById(R.id.start_button);
        user_button.setOnClickListener(this);
        start_button.setOnClickListener(this);
        goodList=new ArrayList<>();
        goodLocations=new ArrayList<>();
        allCheck=(CheckBox)findViewById(R.id.all_checkbox);
        allCheck.setOncheckListener(new CheckBox.OnCheckListener() {
            @Override
            public void onCheck(CheckBox view, boolean check) {
                if(view.isCheck()){
                    for(int i=0;i<goodList.size();i++) {
                        Good good = goodList.get(i);
                        good.setIsSelected(true);
                        goodList.set(i,good);
                    }
                }
                else{
                    for(int i=0;i<goodList.size();i++) {
                        Good good = goodList.get(i);
                        good.setIsSelected(false);
                        goodList.set(i,good);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        Log.d("lyc", "onCreate: init01 is finished");

        initGoods();
        adapter=new GoodAdapter(PurchasingcarActivity.this,
                                            R.layout.good_item,goodList);
        listView=(ListView)findViewById(R.id.good_list_view);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener((AdapterView.OnItemClickListener) new MyOnItemClickedListener());
        Log.d("lyc", "onCreate: init02 is finished");
    }

    @Override
    protected void onResume() {
        super.onResume();
        allCheck.setChecked(false);
        adapter.notifyDataSetChanged();
    }

    private void initGoods(){
        Good good1=new Good("innisfree/悦诗风吟花吻丝绒墨印唇釉哑光雾面滋润丰盈持久",R.drawable.lip,
                            R.drawable.tmall,"innisfree官方旗舰 >",new LatLng(117.182013,36.974136));
        goodList.add(good1);
        Good good2=new Good("完美日记十二色动物眼影盘小狗小猫猫咪老虎斑虎小猪小熊猫小鹿女",R.drawable.eyeshadow,
                R.drawable.jd,"perfectdiary旗舰 >",new LatLng(117.182013,36.974136));
        goodList.add(good2);
        Good good3=new Good("阿迪达斯官网adidas neo LITE RACER男女休闲运动鞋BB9774 DB0577",R.drawable.shoes,
                            R.drawable.tmall,"adidas官方旗舰店 >",new LatLng(117.17983,36.975973));
        goodList.add(good3);
        Good good4=new Good("现货速发 机器学习 周志华 人工智能书籍入门教材书 机器学习基础知识 机器学习方法 机器学习西瓜书 百面机器学习 清华大学出版社",R.drawable.book,
                            R.drawable.taobao,"常青藤图书专营店 >",new LatLng(117.179347,36.975934));
        goodList.add(good4);
        Good good5=new Good("【下单减120】Apple/苹果AirPods 2代无线蓝牙耳机正品原装双配置", R.drawable.airpods,
                R.drawable.suning,"苏宁易购济南王舍人店 >");
        goodList.add(good5);
        Good good6=new Good("女装 宽松连帽运动衫(长袖)(卫衣) 422706 优衣库UNIQLO",R.drawable.sweater,
                R.drawable.taobao,"优衣库官方旗舰店 >");
        goodList.add(good6);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_button:
                fillGoodList();
                setAllGoodIsSelectedOrNot(false);
                Log.d("lyc", "onClick: fillGoodList is done");
                if(goodLocations.size()>0){
                    Intent intent=new Intent(PurchasingcarActivity.this, MapActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(PurchasingcarActivity.this,"尚未选择任何商品！",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.user_button:
                Intent intent1=new Intent(PurchasingcarActivity.this,UserInfoActivity.class);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    class MyOnItemClickedListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Good good=goodList.get(position);
            if(good.getIsSelected()){
                good.setIsSelected(false);
            }
            else{
                good.setIsSelected(true);
            }
            goodList.set(position,good);
            adapter.notifyDataSetChanged();
        }
    }

    private void fillGoodList(){
        Log.d("lyc", "fillGoodList: begin running");
        AdapterView adapterView=(AdapterView)findViewById(R.id.good_list_view);
        View view;
        for(int i=0;i<adapterView.getCount();i++){
            view=adapterView.getChildAt(i);
            Good good=goodList.get(i);
            if(good.getIsSelected()){
                Log.d("lyc", "fillGoodList: "+i+" is added into locationList");
                LatLng newLL=good.getLatLng();
                goodLocations.add(newLL);
            }
        }
        Log.d("lyc", "fillGoodList: done");
    }

    private void setAllGoodIsSelectedOrNot(boolean val){
        Good good;
        for(int i=0;i<goodList.size();i++){
            good=goodList.get(i);
            good.setIsSelected(val);
            goodList.set(i,good);
        }
    }
}
